import i18n from '@/i18n'
import { MetaBoardDataDao } from '@apps/meta/domain/meta-board-data/meta-board-data.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { Page } from '@libs/lhy-core'
import { Body, Controller, Delete, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaOpenApiBoardDataDelDto } from './dto/meta-open-api-board-data-del.dto'
import { MetaOpenApiBoardDataPageDto } from './dto/meta-open-api-board-data-page.dto'
import { MetaOpenApiBoardDataSaveDto } from './dto/meta-open-api-board-data-save.dto'
import { MetaOpenApiBoardDataUpdateByIdDto } from './dto/meta-open-api-board-data-update-by-id.dto'

@ApiTags(i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-0'))
@Controller({ path: 'open_api/board_data', version: '1' })
export class MetaOpenApiBoardDataController {
  constructor(private readonly boardDataDao: MetaBoardDataDao) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-1') })
  @Post('page')
  pageBoardData(@Body() param: MetaOpenApiBoardDataPageDto): Promise<Page<DomainModel.MetaBoardData>> {
    return this.boardDataDao.page(param, {
      platform: param.platform,
      project: param.project,
      label: {
        contains: param.label,
      },
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-2') })
  @Post('board_data')
  async updateById(@Body() param: MetaOpenApiBoardDataUpdateByIdDto) {
    const oldData = Assist.notNull(
      await this.boardDataDao.getById(param.id),
      i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-3', { args: [param.id] })
    )
    Assist.threw(
      oldData.platform !== param.platform,
      i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-4')
    )
    Assist.threw(
      param.project && oldData.project !== param.project,
      i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-5')
    )

    return await this.boardDataDao.updateById(param.id, {
      label: param.label,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-6') })
  @Put('board_data')
  async save(@Body() param: MetaOpenApiBoardDataSaveDto) {
    return await this.boardDataDao.save({
      ...param,
      dataHash: '',
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-open-api.meta-open-api-board-data.12150130-7') })
  @Delete('board_data')
  async del(@Query() param: MetaOpenApiBoardDataDelDto) {
    await this.boardDataDao.deleteByIds([param.id], { platform: param.platform, project: param.project })
  }
}
